<!--组件-参数-->
<template>
  <div>

    <!--参数设置-->
    <div class="row acenter">
      <div class="row acenter" @click="openClick(!isOpen)">
        <text class="font28 main-t1">{{T('参数设置')}}</text>
        <LImage class="arrow-image" :src="isOpen ? config.img('xiala_blue_.png') : config.img('xiala_blue.png')"></LImage>
      </div>

      <div class="all"></div>
      <div class="restore main1" v-if="isRestore" @click="restoreClick">
        <text class="font24 text1">{{T('重置推荐设置')}}</text>
      </div>
    </div>

    <!--参数列表-->
    <div v-if="isOpen">
      <div v-for="(item, index) in datas" v-bind:key="index">
        
        <template v-if="item.visiable != '0'">

          <VParamType class="type" v-if="item.name && types[item.name]"
          v-model="item.value"
          :isEdit="isEdit"
          :types="types[item.name].types"
          :explain="T(item.comment)" 
          :title="T(item.name)" 
          @typeChange="typeChange"></VParamType>

          <VSwitc class="switc" v-else-if="item.name && switcs[item.name]"
          v-model="item.value" 
          :isEdit="isEdit" 
          :explain="T(item.comment)" 
          :title="T(item.name)"></VSwitc>
          
          <!--信号-->
          <VSignalx class="sgnal" v-else-if="item.name && signals[item.name]"
          ref="signal" v-model="item.value" />

          <!-- <VSwitc class="switc" v-else-if="item.name && signal[item.name]"
          v-model="item.value" 
          :isEdit="isEdit" 
          :explain="T(item.comment)" 
          :title="T(item.name)"></VSwitc> -->

          <VInput class="input" v-else
          v-model="item.value" 
          :isEdit="isEdit" 
          :min="item.start"
          :max="item.end"
          :decimal="item.unitName == '%' ? 2 : undefined" 
          :explain="T(item.comment)" 
          :title="T(item.name)" 
          :placeholder="item.option ? T('选填'): T(item.name)" 
          :unit="T(item.unitName)"
          @input="onInput"></VInput>
        </template>
      </div>
    </div>
  </div>
</template>

<script>

//组件
import VInput from "@/public/view/VInput.vue";
import VParamType from "@/up/view/VParamType.vue";
import VSwitc from "@/public/view/VSwitc.vue";
import VSignalx from "@/up/view/VSignalx.vue";

//功能
import math from "@/_js/math";
import modal from "@/_js/modal";
import config from "@/_js/config";

const weexFunction = weex.requireModule("WeexFunction");


import I18N from "@/_i18n/i18n";
var T = I18N.t;

export default {

  components: {
    VParamType,
    VInput,
    VSwitc,
    VSignalx
  },

  model:{
    prop:"isOpen",
    event:"change",
  },

  props:{
    isOpen:Boolean, //turn 显示

    isModify:{ //turn 修改参数 
      type:Boolean,
      default:false,
    },

    isMany:{ //turn 多币对
      type:Boolean,
      default:false,
    },

    isRestore:{  //true 显示“重置推荐设置”
      type:Boolean,
      default:true,
    },

    isEdit:{ //turn可编辑
      type:Boolean,
      default:true,
    },
    
    investCoin:String, //投资币
    reserves:Array, //参数数组
    pairMap:Object, //交易对
  },

  watch: {
    reserves: {
      deep: false,
      handler (val, oldVal) {
        this.initData(this.reserves);
      }
    },

    pairMap: {
      deep: false,
      handler (val, oldVal) {
        this.types = this.initTypes(this.typesx);
        this.initData(this.reserves);
      }
    }
  },
  
  data() {
    return {   
      T:T,   
      math:math,
      config:config,

      datas:[], //修改后的参数

      /** 可选数据
       * 价格上限
       * 价格下限
       * */   
      options:{'价格上限':'1', 
               '价格下限':'1'},
               
      /** 隐藏数据
       * 定投类型
       * */ 
      hides:{'定投类型':'1'},

      /** 隐藏数据-多币对
       * 价格上限
       * 价格下限
       * */ 
      manyHides:{'价格上限':'1', 
                 '价格下限':'1'},
      

      /** 类型参数
       * key 需要处理的参数
       *    * types 类型名数组
       *    * key 需要隐藏的参数
       */
      types:[],
      typesx:{'补仓金额模式':{'types':[T('倍数补仓'), T('增量补仓')],
                           '补仓金额倍数':{'tag':'0'}, 
                           '倍投起始单':{'tag':'0'}, 
                           '补仓金额增量':{'tag':'1'}},

            '补仓间隔模式':{'types':[T('固定间隔'), T('自定义间隔')],
                          '间隔比例':{'tag':'0'}, 
                          '补仓增幅':{'tag':'0'}, 
                          '第1次补仓比例':{'tag':'1'},
                          '第2次补仓比例':{'tag':'1'},
                          '第3次补仓比例':{'tag':'1'},
                          '第4次补仓比例':{'tag':'1'},
                          '第5次补仓比例':{'tag':'1'},
                          '第6次补仓比例':{'tag':'1'},
                          '第7次补仓比例':{'tag':'1'},
                          '第8次补仓比例':{'tag':'1'}},

            '补仓下单模式':{'types':[T('追踪补仓'), T('预埋单补仓')],
                          '补仓反弹比例':{'tag':'0'},
                          '同时开仓单数':{'tag':'1'}},

            '网格分布':{'types':[T('等比网格'), T('等差网格')],
                      '网格间隔(等比)':{'tag':'0'}, 
                      '网格间隔(等差)':{'tag':'1'}},

            '止盈平仓模式':{'types':[T('一次性止盈'), T('逐单止盈')]},

            '止盈下单模式':{'types':[T('追踪止盈'), T('预埋单止盈')],
                          '止盈回撤比例':{'tag':'0'}},

            
            '利润模式':{'types':[T('赚${baseCoin}'), T('赚${counterCoin}')]},

            
            '止盈仓位模式':{'types':[T('一次性止盈'), T('逐单止盈')]},

            
            '首单开仓模式':{'types':[T('时间触发'), T('信号触发')],
                          '开仓等待时长':{'tag':'0'},
                          '开仓信号':{'tag':'1'}}
            },

      switcs:{'开启追踪开仓':'1'},


      signals:{'开仓信号':'1'},
      signal:undefined, //信号设置
    };
  },

  mounted(){
    this.types = this.initTypes(this.typesx);
    this.initData(this.reserves);
  },

  methods:{

    initTypes(datas) {
      var types = JSON.parse(JSON.stringify(datas));
      for (const key in types) {
        if (Object.hasOwnProperty.call(types, key)) {
          const type = types[key];
          for (let index = 0; index < type.types.length; index++) {
            var str = type.types[index];
            str = str.replace('${baseCoin}', this.pairMap.baseCoin);
            str = str.replace('${counterCoin}', this.pairMap.counterCoin);
            type.types[index] = str;
          }
        }
      }
      return types;
    },
    
    //初始化数据
    initData(datas) {

      var reserves = JSON.parse(JSON.stringify(datas));
      for (let index = 0; index < reserves.length; index++) {

        var reserve = reserves[index];

        if (reserve.name && !this.signals[reserve.name]) {
          reserve.value = reserve.value != undefined ? (reserve.value + '') : '';
        }
        reserve.start = reserve.start != undefined ? (reserve.start + '') : '';
        reserve.end = reserve.end != undefined ? (reserve.end + '') : '';

        //1、百分比类型参数
        if (reserve.valueType && reserve.valueType == 'percent') {
          reserve.unitName = '%';
        }

        //2、价格参数
        if (math.getNumber(reserve.valueMax) != 0 || 
            math.getNumber(reserve.valueMin) != 0) {
              
            var price = this.investCoin == this.pairMap.baseCoin ? this.pairMap.baseCoinUsdPrice : this.pairMap.usdPrice;
            var decimal = this.investCoin == this.pairMap.baseCoin ? this.pairMap.baseCoinDecimal : this.pairMap.counterCoinDecimal;
            decimal = math.getNumber(decimal);
            reserve.start = math.chu(reserve.valueMin, price, decimal, 2);
            reserve.end = math.chu(reserve.valueMax, price, decimal, 2);

            //编辑状态下，默认值不折合
            if (!this.isModify) {
              reserve.value = math.chu(reserve.value, price, decimal, 2);
            }

            reserve.unitName = this.investCoin;
        }

        //3、处理可选参数
        if (reserve.name && this.options[reserve.name]) {
          reserve.option = true;
          reserve.value = math.getNumber(reserve.value) == 0 ? '' : reserve.value;
        }
        
        //4、隐藏参数
        if (reserve.name) {
          //全隐藏
          if (this.hides[reserve.name]) {
            reserve.visiable = '0';
          } 
          //多币对隐藏
          else if (this.manyHides[reserve.name]) {
            reserve.visiable = this.isMany ? '0' : '1';
          }
        }
        
        //类型参数
        this.initType(reserves, reserve);
      }
      this.datas = reserves;
      this.$emit("paramChange");
    },

    //初始化类型参数
    initType(reserves, reserve) {

      if (reserve.name && this.types[reserve.name]) {

        var type = this.types[reserve.name];
        reserve.types = type.types;
        for (let index = 0; index < reserves.length; index++) {

          var map = reserves[index];

          //隐藏参数不用处理
          if (map.name && this.hides[map.name]) {
            continue;
          }

          //处理不同类型，显影不同参数
          if (map.name && type[map.name]) {

            var dict = type[map.name];
            map.visiable = reserve.value == dict.tag ? '1' : '0';

            //取消还原逻辑 2021.6.15
            // if (dict.value == undefined) {
            //   dict.value = map.value;
            // } else {
            //   map.value = dict.value;
            // }
          }
        }
      }
    },
    
    //检查
    checkError(){
 
      for (let index = 0; index < this.datas.length; index++) {
        var data = this.datas[index];

        //隐藏数据--不检查
        if(data.visiable && data.visiable == '0') {
          continue;
        }

        //可选参数--可不输入
        if(data.option && data.value.length == 0) {
          continue;
        }
        
        //信号参数-未选择
        if(data.name && this.signals[data.name] && !data.value.id) {
          modal.toast(T('请选择&{1}').replace("&{1}", T(data.name)));
          return true;
        }

        //未输入
        if (data.value.length == 0) {
          modal.toast(T('请输入&{1}').replace("&{1}", T(data.name)));
          return true;
        } 

        //无范围值-不校验值
        if (data.start == null || 
                data.start.length == 0 || 
                data.end == null || 
                data.end.length == 0) {
          continue;
        } 
        
        //不在范围内
        if (math.getNumber(data.value) < math.getNumber(data.start) ||
                 math.getNumber(data.value) > math.getNumber(data.end)) {
          modal.toast(T('&{1}不在范围内').replace("&{1}", T(data.name)));
          return true;
        }
      }
      return false;
    },
    
    //获取参数
    getParam(){
      
      var datas = JSON.parse(JSON.stringify(this.datas));
      var params = [];
      for (let index = 0; index < datas.length; index++) {
        var data = datas[index];

        //处理可选参数
        if (data.optional && data.value.length == 0) {
          data.value = "0";
        } 

        // //处理开关参数
        // else if (data.name && this.switcs[data.name]) {
        //   data.value = Number(data.value)+'';
        // }

        else if(data.name && !this.signals[data.name]) {
          data.value = Number(data.value) +'';
        }
        
        //特殊处理，合约没有这个控制参数，需要删除对应参数
        if (data.name == '网格分布') {
          var data1 = datas[index+1];
          data1.value = (data1.visiable != '0') ? data1.value : '0';
          var data2 = datas[index+2];
          data2.value = (data2.visiable != '0') ? data2.value : '0';
        }

        params.push(data);
      }

      return params;
    },

    /**********************************************输入框回调*/
    //输入内容改变
    onInput(e){
      this.$emit("paramChange");
    },

    /********************************** 点击触发*/
    //展开点击
    openClick(isOpen) {
      this.isOpen = isOpen;
      this.$emit('change', isOpen);
    },

    //重置推荐设置
    restoreClick() {
      this.initData(this.reserves);
    },

    //类型参数点击
    typeChange() {
      for (let index = 0; index < this.datas.length; index++) {
        var reserve = this.datas[index];
        this.initType(this.datas, reserve);
      }
    },
  }
}
</script>


<style src="@/_css/style.css"></style>
<style scoped>
.arrow-image{
  width: 20px;
  height: 20px;
  margin-left: 20px;
  margin-right: 20px;
}
.restore {
  flex-direction: row;
  align-items: center;
  height: 48px;
  border-radius: 24px;

  padding-left: 20px;
  padding-right: 20px;
}
.input {
  margin-top: 40px;
}
.type {
  margin-top: 40px;
}
.switc {
  margin-top: 40px;
}
.sgnal {
  margin-top: 40px;
}
</style>
